var app = getApp()
var IP = app.globalData.IP
var dataformat = require('../../../utils/dataformat')
Page({
  data: {
    active: 0,
    orderlist: [],
    serviceinfo: [],
  },

  onLoad(options) {

    if (options?.active) {
      console.log(options?.active)
      console.log("wwwwwwwwww")
      this.setData({
        active: Number(options.active)
      })

    } else if (options?.getallorders == 'yes') {
      this.getAllorders()
      console.log('cccccccc')
    } else if (this.data.active == 0) {
      this.getAllorders()
      console.log('aaaaaaaa')
    } else {
      console.log(this.data.active)
      this.getorderlistbystatus(this.data.active)
      console.log('bbbbbbbbb')
    }






  },

  onChange(event) {
    var status = event.detail.name

    if (status == 0) {
      this.getAllorders()
      this.setData({
        active: 0
      })
      console.log(this.data.active)
    } else {
      this.setData({
        active: status
      })
      this.getorderlistbystatus(status)

    }

  },

  //取消订单
  updateStatusTo5(e) {
    wx.request({
      url: `http://${IP}:8080/order/updateStatusTo5/` + e.currentTarget.dataset.id,
      success: res => {

        this.onLoad()


      }

    })

  },


  deleteorder(e) {
    wx.showModal({
      title: '',
      content: '确认删除订单？',
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {
          wx.request({
            url: `http://${IP}:8080/order/deleteorder/` + e.currentTarget.dataset.id,
            success: res => {

              this.onLoad()

            }

          })
        }
      }
    })


  },
  // 重新下单
  updateStatusTo1(e) {

    wx.request({
      url: `http://${IP}:8080/order/updateStatusTo1/` + e.currentTarget.dataset.id,
      success: res => {

        this.onLoad()

      }

    })

  },

  // 立即评价
  evaluate(e) {
    wx.navigateTo({
      url: '/pages/order/evaluate/evaluate',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('sendoptionstoevaluate', {
          orderid: e.currentTarget.dataset.orderid,
          imageurl: e.currentTarget.dataset.imageurl,
          serviceid: e.currentTarget.dataset.serviceid,
          id: e.currentTarget.dataset.id,
        })

      }
    })

  },

  //获取不同状态订单 statu=1未接单 2待上门  3待评价 4已完成 5 已取消
  getorderlistbystatus(status) {

    wx.request({
      url: `http://${IP}:8080/order/getorderlistbystatus/` + status,
      success: res => {
        this.setData({
          orderlist: res.data
        })

        // 格式化时间
        this.data.orderlist.forEach((item) => {

          item.ordertime = dataformat.formatTime(item.ordertime, 'YY-MM-DD hh-mm')
        })
        this.setData({
          orderlist: this.data.orderlist
        })



        // console.log(this.data.orderlist)
        //根据获取的id依次同步请求获取服务图片
        this.getserviceImage()
      }
    })


  },

  getAllorders() {
    wx.request({
      url: `http://${IP}:8080/order/getAllOrderlist`,
      success: res => {
        this.setData({
          orderlist: res.data
        })
        // 格式化时间
        this.data.orderlist.forEach((item) => {

          item.ordertime = dataformat.formatTime(item.ordertime, 'YY-MM-DD hh-mm')
        })
        this.setData({
          orderlist: this.data.orderlist
        })
        // console.log(this.data.orderlist)
        // console.log("getallorders")


        //根据获取的id依次同步请求获取服务图片
        this.getserviceImage()
      }
    })

  },
  //同步请求获取对应服务图片
  async getserviceImage() {
    this.setData({
      serviceinfo: []
    })
    var list = this.data.orderlist
    for (var i = 0; i < list.length; i++) {
      await new Promise((resolve, reject) => {
        wx.request({
          url: `http://${IP}:8080/service/getservicebyserviceid/` + list[i].serviceid,
          success: res => {
            this.setData({
              serviceinfo: this.data.serviceinfo.concat(res.data),
            })
            resolve();
          }
        })

      })

    }

  },



})